import location
import userProfile

from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app

def distance(location, user):
    return int((location.shoppingScale - user.shoppingScale) ** 2 + (location.historyScale - user.historyScale) ** 2 + (location.ecologyScale - user.ecologyScale) ** 2 + (location.gastronomyScale - user.gastronomyScale) ** 2 + (location.cultureScale- user.cultureScale) ** 2 + (location.nightLifeScale - user.nightLifeScale) ** 2 )

def locationCmp(location1, location2, user):
    if distance(location1, user) > distance(location2, user):
        return 1
    elif distance(location1, user) == distance(location2, user):
        return 0
    else:
        return -1


class GuidePage(webapp.RequestHandler):
    def get(self):
        self.response.out.write("<html><head>")
        self.response.out.write("<p><b><font size=\"5\">Creating a city tour</font></b></p>")
        users = db.GqlQuery("SELECT * FROM UserProfile ORDER BY name ASC")
        self.response.out.write("""
            <form action="/guide/create" method="post">
            <table border="0" width="350" id="table">
            <tr>
            <td align="right"> <b> Select the user: </b> </td>
            <td> <select name="userCombo" size="1">
            <option value=""> Choose one </option>
            """)
        
        for user in users:
            self.response.out.write("<option value=\"")
            self.response.out.write(user.key())
            self.response.out.write("\">")
            self.response.out.write(user.name)
            self.response.out.write("</option>")

        self.response.out.write("""
            </td>
            </tr>
            <tr>
            <td align="right"> <b> Tour maximum time: </b> </td>
            <td> <input type="text" name="time" size="4"> &nbsp; minutes </td>
            </tr>
            <tr>
            <td></td>
            <td align="left"> <input type="submit" value="Create"> </td>
            </tr>
            </table> </form>
            """)
        self.response.out.write("</head></html>")


class CreateTour(webapp.RequestHandler):
    def post(self):
        user = db.get(self.request.get('userCombo'))
        query = db.GqlQuery("SELECT * FROM Location")
        locations = []

        for item in query:
            locations.append(item)

        locations.sort(lambda x, y : locationCmp(x,y,user))

        route = []
        maxTime = int(self.request.get('time'))
        time = 0
        i = 0

        while ( ( time < maxTime ) & ( i < len(locations) ) ):
            route.append(locations[i])
            time += locations[i].time
            i += 1

        self.response.out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
        self.response.out.write("<tour>\n")

        for place in route:
            self.response.out.write("<location>\n")
            self.response.out.write("<name>\n")
            self.response.out.write(place.name)
            self.response.out.write("\n</name>\n")
            self.response.out.write("<address>\n")
            self.response.out.write(place.address)
            self.response.out.write("\n</address>\n")
            self.response.out.write("<time>\n")
            self.response.out.write(place.time)
            self.response.out.write("\n</time>\n")
            self.response.out.write("</location>\n")
            

        self.response.out.write("</tour>\n")
        
                                


application = webapp.WSGIApplication([('/guide', GuidePage), ('/guide/create', CreateTour)], debug=True)


def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
